package day01;

import com.blackhorse.domain.QueryVo;
import com.blackhorse.domain.User;
import com.blackhorse.xml.dao.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

public class Demo1_CRUD {
    private SqlSessionFactory sqlSessionFactory;
    private SqlSession sqlSession;
    private UserMapper mapper;

    //初始化操作，读取配置文件构建SqlSessionFactory和SqlSession
    @Before
    public void init() {
        try {
            InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
            SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
            sqlSessionFactory = builder.build(in);
            sqlSession = sqlSessionFactory.openSession();
            mapper = sqlSession.getMapper(UserMapper.class);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    //结束操作，提交事务，释放资源
    @After
    public void destory() {
        sqlSession.commit();
        if (sqlSession != null) {
            sqlSession.close();
        }
    }

    @Test
    public void testFindAll() {
        List<User> users = mapper.findAll();
        for (User user : users) {
            System.out.println(user);
        }
    }

    @Test
    public void testSaveUser() {
        User user = new User();
        user.setUsername("张大三");
        user.setBirthday(new Date());
        user.setSex("男");
        user.setAddress("河南省郑州市xx区xx路1号");
        System.out.println("插入前id:" + user.getId());
        mapper.saveUser(user);
        System.out.println("插入后id:" + user.getId());
    }

    @Test
    public void testUpdateUser() {
        User user = new User();
        user.setId(7);
        user.setUsername("李三三");
        user.setBirthday(new Date());
        user.setSex("男");
        user.setAddress("河南省郑州市xx区xx路1号");
        mapper.updateUser(user);
    }

    @Test
    public void testDeleteUserById() {
        mapper.deleteUserById(7);
    }

    @Test
    public void testFindById() {
        User user = mapper.findById(2);
        System.out.println(user);
    }


    @Test
    public void testFindByName() {
        //使用#{value}占位符时需要在参数中加上% %
//        List<User> users = mapper.findByName("%王%");
//        for (User user : users) {
//            System.out.println(user);
//        }

        //使用${value}拼接参数时在sql中加上% %
        List<User> users = mapper.findByName("王");
        for (User user : users) {
            System.out.println(user);
        }

    }

    @Test
    public void testFindTotal() {
        int total = mapper.findTotal();
        System.out.println("总记录数：" + total);
    }

    //pojo包装对象作为查询条件
    @Test
    public void testFindUserByVo() {
        QueryVo vo = new QueryVo();
        User u = new User();
        u.setUsername("%王%");
        vo.setUser(u);
        List<User> users = mapper.findUserByVo(vo);
        for (User user : users) {
            System.out.println(user);
        }
    }

}
